package com.tanger.common.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * @Description 对象拷贝
 * @Author Hchenbin
 * @Date 2022/5/9 9:26
 * @Version 1.0
 */
public final class BeanUtils {

    public static <E> E copyProperties(Object source, E target) {
        BeanUtil.copyProperties(source, target, CopyOptions.create()
                .setIgnoreError(true).setIgnoreNullValue(true));
        return target;
    }

    public static <E> E copyProperties(Object source, E target, String... prpperties) {
        BeanUtil.copyProperties(source, target, CopyOptions.create()
                .setIgnoreError(true).setIgnoreNullValue(true).setIgnoreProperties(prpperties));
        return target;
    }

    public static boolean hasNullField(Object o) throws IllegalAccessException {
        for (Field field : o.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            Object fieldValue = field.get(o);
            if (Objects.isNull(fieldValue)) {
                return true;
            }
        }
        return false;
    }
}
